package com.bjpowernode.service.impl;

import com.bjpowernode.constant.Constants;
import com.bjpowernode.entity.TProductDO;
import com.bjpowernode.manager.RedisManager;
import com.bjpowernode.mapper.TProductMapper;
import com.bjpowernode.service.TProductService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 *
 */
@Service
public class TProductServiceImpl implements TProductService {

    @Resource
    private TProductMapper tProductMapper;

    @Resource
    private RedisManager redisManager;

    @Override
    public Map<Integer, String> selectAllIdAndName() {
        List<TProductDO> tProductDOList = tProductMapper.selectAllIdAndName();
        Map<Integer , String> tProductDOMap = new HashMap<>();
        for (TProductDO tProductDO : tProductDOList) {
            tProductDOMap.put(tProductDO.getId(), tProductDO.getName());
        }
        return tProductDOMap;
    }

    @Override
    public Map<String, Integer> selectAllNameAndId() {
        List<TProductDO> tProductDOList = tProductMapper.selectAllIdAndName();
        Map<String , Integer> tProductDOMap = new HashMap<>();
        for (TProductDO tProductDO : tProductDOList) {
            tProductDOMap.put(tProductDO.getName(), tProductDO.getId());
        }
        return tProductDOMap;
    }

    @Override
    public List<TProductDO> loadAllProductDo() {
        return redisManager.getData(() -> {
            return (List<TProductDO>)redisManager.getValue(Constants.REDIS_PRODUCT_KEY);
        } , () -> {
            return tProductMapper.selectAllIdAndName();
        } , (t)-> {
            redisManager.setValue(Constants.REDIS_PRODUCT_KEY , t , 5 , TimeUnit.MINUTES);
        });
    }
}
